package com.tanhua.dubbo.server.api;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.tanhua.dubbo.server.pojo.Users;
import com.tanhua.dubbo.server.vo.PageInfo;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@Service(version = "1.0.0")
public class UsersApiImpl implements UsersApi {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public String saveUsers(Long userId, Long friendId) {
        //先判断传递过来的数据是否都为null
        if (!ObjectUtil.isAllNotEmpty(userId, friendId)) {
            return null;
        }
        //检测是否有好友关系 去数据库里面查询
        //查询条件
        Query query = Query.query(Criteria.where("userId").is(userId).and("friendId").is(friendId));
        long count = this.mongoTemplate.count(null, Users.class);
        if (count > 0) {
            return null;
        }
        //保存我和好友的数据
        Users users = new Users();
        users.setId(ObjectId.get());
        users.setDate(System.currentTimeMillis());
        users.setUserId(userId);
        users.setFriendId(friendId);
        this.mongoTemplate.save(users);
//保存好友和我的数据
        users.setId(ObjectId.get());
        users.setUserId(friendId);
        users.setFriendId(userId);
        this.mongoTemplate.save(users);
        //数据保存好了 就可以返回了
        return users.getId().toHexString();

    }

    /**
     * 删除好友关系
     *
     * @param userId   用户id
     * @param friendId 好友id
     * @return
     */
    @Override
    public Boolean removeUsers(Long userId, Long friendId) {
        //不用查询是否为好友关系  因为要删除必须是好友关系
        //查询条件
        Query query = Query.query(Criteria.where("userId").is(userId).and("friendId").is(friendId));
        //删除我月好友的关系数据
        long count = this.mongoTemplate.remove(query, Users.class).getDeletedCount();


        Query query1 = Query.query(Criteria.where("userId").is(friendId).and("friendId").is(userId));
        //删除好友与我的关系数据
        long count1 = this.mongoTemplate.remove(query1, Users.class).getDeletedCount();
        return count > 0 && count1 > 0; //要两边都是删除成功才真正的删除成功
    }
    /**
     * 根据用户id查询全部Users列表
     *
     * @param userId
     * @return
     */
    @Override
    public List<Users> queryAllUsersList(Long userId) {
        //查询条件
        Query query = Query.query(Criteria.where("userId").is(userId));
        List<Users> users = this.mongoTemplate.find(query, Users.class);
        return users;
    }
    /**
     * 根据用户id查询Users列表(分页查询)
     *
     * @param userId
     * @return
     */
    @Override
    public PageInfo<Users> queryUsersList(Long userId, Integer page, Integer pageSize) {
        PageRequest pageRequest = PageRequest.of(page - 1, pageSize, Sort.by(Sort.Order.desc("created")));
        Query query = Query.query(Criteria.where("userId").is(userId)).with(pageRequest);
        List<Users> usersList = this.mongoTemplate.find(query, Users.class);
        PageInfo<Users> pageInfo=new PageInfo<>();
        pageInfo.setPageNum(page);
        pageInfo.setPageSize(pageSize);
        pageInfo.setRecords(usersList);
        return pageInfo;
    }



}
